package com.example.CarManagement.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.CarManagement.common.Result;
import com.example.CarManagement.sys.entity.CarInfo;
import com.example.CarManagement.sys.entity.LoginUser;
import com.example.CarManagement.sys.entity.User;
import com.example.CarManagement.sys.mapper.CarInfoMapper;
import com.example.CarManagement.sys.service.ICarInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author 徐佳彪
 * @since 2025-07-08
 */
@Service
public class CarInfoServiceImpl extends ServiceImpl<CarInfoMapper, CarInfo> implements ICarInfoService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private UserServiceImpl userService;

    // 分页查询
    public Result<?> getList(Integer pageNo, Integer pageSize, String plateNumber, Integer status) {
        LambdaQueryWrapper<CarInfo> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(plateNumber)) {
            queryWrapper.like(CarInfo::getPlateNumber, plateNumber);
        }
        if (status != null) {
            queryWrapper.eq(CarInfo::getStatus, status);
        }
        queryWrapper.orderByDesc(CarInfo::getCreateTime);

        Page<CarInfo> carInfoPage = this.baseMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);

        Map<String, Object> data = new HashMap<>();

        data.put("list", carInfoPage.getRecords());
        data.put("total", carInfoPage.getTotal());

        return Result.success(data);
    }

    @Override
    public Result<?> addCarInfo(CarInfo carInfo, String token) {

        // 查询车牌号是否重复
        String plateNumber = carInfo.getPlateNumber();

        LambdaQueryWrapper<CarInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarInfo::getPlateNumber, plateNumber);
        CarInfo oldCarInfo = this.baseMapper.selectOne(queryWrapper);
        if (oldCarInfo != null) {
            return Result.fail("车牌号已存在");
        }

        carInfo.setId(UUID.randomUUID().toString());

        // 获取当前用户
        LoginUser userInfo = userService.getUserInfo(token);

        System.out.println(userInfo);

        // 设置创建用户
        carInfo.setCreateBy(userInfo.getUserId());

        // 设置创建时间
        carInfo.setCreateTime(LocalDateTime.now());

        // 设置更新时间
        carInfo.setUpdateTime(LocalDateTime.now());

        // 设置更新人
        carInfo.setUpdateBy(userInfo.getUserId());

        this.baseMapper.insert(carInfo);

        return Result.success("添加成功");
    }

    @Override
    public Object getCarSelector() {
        LambdaQueryWrapper<CarInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarInfo::getStatus, 0);

        List<CarInfo> carInfos = this.baseMapper.selectList(queryWrapper);


        List<Map<String, Object>> data = new ArrayList<>();

        for (CarInfo carInfo : carInfos) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", carInfo.getId());
            map.put("label", carInfo.getPlateNumber());
            data.add(map);
        }

        return Result.success(data);
    }


}
